<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News_Model extends CI_Model{
    function __construct()
    {
        parent::__construct();
    }
    function getAllData($num,$offset){
        $query = $this->db->get('news',$num,$offset);
      
        return $query->result();  
    } 
     function get_object($id)
    {
        $query = $this->db->get_where('news', array('news_id' =>$id));
        return $query->row();
    }
    function getNumData(){
        $query = $this->db->get('news');
        return $query->num_rows();
    }
    function delete($id){
          $this->db->where('news_id',$id);
          $query = $this->db->delete('news');
          if($query){
              return true;
          }else{
              return false;
          }
    }
	function deleted($id){
          $this->db->where('news_id',$id);
		  $data=array('bl_active'=>'-1');
          $query = $this->db->update('news',$data);
          if($query){
              return true;
          }else{
              return false;
          }
    }
    function get_list_permit(){
        return $this->db->get('permission')->result();
    }
   
   function saveData($news_id,$data){
             if($news_id!=0){
                    $this->db->where('news_id',$news_id);
                    if($this->db->update('news',$data)){
                         return true;
                    }else{
                         return false;
                    }
             }else{
                    if($this->db->insert('news',$data)){
                        return true;
                        
                    }else{
                        return false;
                    }
            }
    }
    function get_num_data($arr_search){
            /*Begin search*/
            switch($arr_search['field_search'])
            {
                    case 'title':
                            $this->db->like('title',$arr_search['key_search']);
                            break ;
					case 'name':
					$this->db->like('category_news.name',$arr_search['key_search']);
					break ;
                    
            }
            /*End search*/
            #$this->db->where('bl_active',1);
			$this->db->join('category_news','news.category_news_id=category_news.category_news_id');
            $query = $this->db->get('news');
             return $query->num_rows();
    }
    function get_all_data($num,$offset,$arr_search)
    {
           $this->db->select('news.*,category_news.name,category_news.category_news_id');
		    /*Begin search*/
            switch($arr_search['field_search'])
            {
					case 'name':
					$this->db->like('category_news.name',$arr_search['key_search']);
					break ;
					case 'title':
                            $this->db->like('title',$arr_search['key_search']);
                            break ;
                    
            }
            /*End search*/
			
			$this->db->join('category_news','news.category_news_id=category_news.category_news_id');
            $this->db->where('news.bl_active <>',-1);
            $this->db->order_by('dt_create','DESC');
            $query = $this->db->get('news',$num,$offset);
             return $query->result();
    }
    function getCategoryName($category_id=0) {
        $this->db->select("name");
        $this->db->where('category_news_id',$category_id);
        $query = $this->db->get("category_news");
        return $query->row();
    }
    
    
  }
?>
